<div class="response-questionnaire__single-matrix">
  <table>
    <thead>
      <tr>
        <td></td>
        <% response.question.response_options.each_with_index do |response_option, idx| %>
          <th><%= translated_attribute(response_option.body) %></th>
        <% end %>
      </tr>
    </thead>
    <tbody>
      <% response.question.matrix_rows.by_position.each_with_index do |row, row_idx| %>
        <tr class="js-radio-button-collection">
          <td><%= translated_attribute row.body %></td>
          <% response.question.response_options.each_with_index do |response_option, idx| %>
            <% choice = response.choices.find { |choice| choice.response_option_id == response_option.id && choice.matrix_row_id == row.id } %>
            <% choice_id = "#{field_id}_matrix_row_#{row_idx}_choice_#{idx}" %>
            <td>
              <div class="js-collection-input">
                <%= radio_button_tag "questionnaire[responses][#{response_idx}][choices][#{row_idx}][body]",
                                     translated_attribute(response_option.body),
                                     response_option.id == choice.try(:response_option_id),
                                     "aria-label": translated_attribute(response_option.body),
                                     id: "#{choice_id}_body", disabled: %>
                <%= hidden_field_tag "questionnaire[responses][#{response_idx}][choices][#{row_idx}][response_option_id]",
                                     response_option.id,
                                     id: "#{choice_id}_response_option",
                                     disabled: true %>
                <%= hidden_field_tag "questionnaire[responses][#{response_idx}][choices][#{row_idx}][matrix_row_id]",
                                     row.id,
                                     id: "#{choice_id}_matrix_row",
                                     disabled: true %>
                <% if response_option.free_text %>
                  <%= text_field_tag "questionnaire[responses][#{response_idx}][choices][#{row_idx}][custom_body]",
                                     choice.try(:custom_body),
                                     id: "#{choice_id}_custom_body",
                                     disabled: true,
                                     data: { controller: "character-counter" },
                                     maxlength: %>
                <% end %>
              </div>
            </td>
          <% end %>
        </tr>
      <% end %>
    </tbody>
  </table>
</div>
